Get Free Disk Space in Bytes
Description
The get_free_disk_space_in_bytes
function retrieves the available free disk space in bytes on the root file system. This can be useful for checking disk usage and ensuring there is enough space for operations.
Function Signature:
def get_free_disk_space_in_bytes() -> int:
Parameters
- None
Returns
- int: The function returns the available disk space in bytes if successful, or
-1
if an error occurred.
Example Usage
free_space = get_free_disk_space_in_bytes()
if free_space != -1:
print(f"Free disk space: {free_space} bytes")
else:
print("Error retrieving free disk space.")
Notes
- The function uses
os.statvfs
to get information about the file system where the root (/
) is mounted. - It calculates the available space using the number of available blocks and the block size from
os.statvfs
. - The function returns the available space in bytes, which can be converted to other units such as kilobytes (KB), megabytes (MB), or gigabytes (GB) if needed.
Error Handling
- If any error occurs while retrieving the disk space (e.g., permission issues, or filesystem errors), the function returns
-1
.